package t02;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DeckOfCards {
    private List<Card> cards;

    public DeckOfCards() {
        this.cards=new ArrayList<>();
        initializeDeck();
    }

    /**
     *  初始化牌组
     */
    private void initializeDeck() {
        String[] suits={"Clubs","Diamonds","Hearts","Spades"};
        String[] values = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
        for (String suit : suits) {
            for (String value : values) {
                cards.add(new Card(suit, value));
            }
        }
    }

    /**
     * 洗牌
     */
    public void shuffle(){
        Collections.shuffle(cards);
    }

    /**
     * 发牌
     * @param numOfPlayers 玩家数量
     * @param cardPerPlayer 每个玩家手中牌的数量
     * @return
     */
    public List<List<Card>> dealHands(int numOfPlayers,int cardPerPlayer){
        //检查剩余的牌是否足够发给所有玩家
        if (numOfPlayers*cardPerPlayer>cards.size()){
            throw new IllegalArgumentException("Not enough cards in the deck for this deal");
        }
        //用于存储所有玩家的手牌
        List<List<Card>> hands=new ArrayList<>();
        //遍历所有玩家
        for (int i = 0; i < numOfPlayers; i++) {
            //存储当前玩家的手牌
            List<Card> hand=new ArrayList<>();
            //将指定数量的牌发给当前玩家
            for (int j = 0; j < cardPerPlayer; j++) {
                //从牌组中移除一张牌，并将其添加到当前玩家的手牌中。
                hand.add(cards.remove(0));
            }
            //将当前玩家的手牌添加到总体的手牌列表中
            hands.add(hand);
        }
        //返回包含所有玩家手牌的列表
        return hands;
    }
}
